package cn.com.surker.system.controller;

import cn.com.surker.common.AbstractController;
import cn.com.surker.dto.system.SysStorageMatchDTO;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.lab.entity.LabExpendUploadLine;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.SysStorageMatchService;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.RollbackException;
import java.util.List;

/**
 * 工控仓位关系关系控制层
 */
@RestController
@RequestMapping("/system/storage/match")
public class SysStorageMatchController extends AbstractController {

    @Resource
    private SysStorageMatchService sysStorageMatchService;


    @PostMapping(value = "/page")
    @ApiOperation(value = "分页方法", tags = "工控仓位关系")
    public RestResultDto pageData(@RequestBody SysStorageMatchDTO dto){
        return RestResultDto.success(sysStorageMatchService.page(dto));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "新增方法", tags = "工控仓位关系")
    @Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
    public RestResultDto save(@RequestBody SysStorageMatchDTO dto){
        try {

            CacheUser user = getUser();
            return sysStorageMatchService.add(dto,user);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新编辑方法", tags = "工控仓位关系")
    @Transactional(rollbackFor = RollbackException.class,propagation = Propagation.REQUIRED)
    public RestResultDto update(@RequestBody SysStorageMatchDTO dto){
        try {

            CacheUser user = getUser();
            return sysStorageMatchService.edit(dto,user);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    @PostMapping(value = "/updatestatus")
    @ApiOperation(value = "更新状态方法", tags = "工控仓位关系")
    @Transactional(rollbackFor = RollbackException.class,propagation = Propagation.REQUIRED)
    public RestResultDto updatestatus(@RequestBody SysStorageMatchDTO dto){
        try {

            CacheUser user = getUser();
            return sysStorageMatchService.editStatus(dto.getId(),dto.getStatus(),user);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    @PostMapping("/getstorage")
    @ApiOperation(value = "根据公司标识查询erp仓位表", tags = "工控仓位关系")
    public RestResultDto getStorageList(Long orgId, Long lineId){
        return RestResultDto.success(sysStorageMatchService.getStorageList(orgId, lineId));
    }

    @PostMapping("/getMainMaterial")
    @ApiOperation(value = "根据公司标识查询材料表", tags = "工控仓位关系")
    public RestResultDto getMainMaterialList(Long orgId, Integer storageType, Long materialId){
        return RestResultDto.success(sysStorageMatchService.getMainMaterialList(orgId, storageType, materialId));
    }

    @PostMapping("/getControlStorage")
    @ApiOperation(value = "根据公司标识查询工控", tags = "工控仓位关系")
    public RestResultDto getControlStorageList(Long orgId, Long lineId){
    	return RestResultDto.success(sysStorageMatchService.getControlStorageList(orgId, lineId));
    }

    @PostMapping("/getLine")
    @ApiOperation(value = "根据公司标识查询生产线数据", tags = "工控仓位关系")
    public RestResultDto getLineList(Long orgId){
    	return RestResultDto.success(sysStorageMatchService.getLineList(orgId));
    }

}
